package LoginFace;

import MainFace.MainJframe;
import Tool.InitJdialog;
import Tool.MySQL_IsUserLoginTrue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.*;

public class LoginJframe extends JFrame implements MouseListener {
    //设置组件
    //按钮
    JButton Loginbutton1 = new JButton(new ImageIcon("Image/Login/登录按钮2.png"));
    JButton Loginbutton2 = new JButton(new ImageIcon("Image/Login/登录按钮1.png"));
    //输入框
    JTextField UserText = new JTextField();
    JPasswordField MimaText = new JPasswordField();

    //获取数据库连接
    String url = "jdbc:mysql:///staffsystem?userSSL=false";
    String username = "root";
    String password = "123456";
    Connection conn = DriverManager.getConnection(url,username,password);


    public LoginJframe() throws Exception {

        //1.登录界面初始化
        InitLoginjframe();

        //2.初始化内容
        InitImage();

        //3.设置界面显示
        this.setVisible(true);
    }

    //1.初始化内容
    private void InitImage() {

        //1.1创建对象
            //图片
        ImageIcon BackgroundIcon = new ImageIcon("Image/Login/登录背景.png");
        ImageIcon UserIcon = new ImageIcon("Image/Login/用户.png");
        ImageIcon MimaIcon = new ImageIcon("Image/Login/密码.png");



            //创建容器
        JLabel BackgroundLaber = new JLabel(BackgroundIcon);
        JLabel UserLaber = new JLabel(UserIcon);
        JLabel MimaLaber = new JLabel(MimaIcon);

            //文字
        JLabel Login = new JLabel("欢迎来到院职工工资管理系统！");
        JLabel LoginText = new JLabel("Login...");

        //1.2设置初始位置
            //图片
        BackgroundLaber.setBounds(-275,0,1320,720);
        UserLaber.setBounds(670,195,25,25);
        MimaLaber.setBounds(670,235,25,25);

            //文字
        Login.setBounds(150,0,1000,100);
        Login.setFont(new Font("",1,45));
        LoginText.setBounds(770,330,100,20);
        LoginText.setFont(new Font("",2,15));

            //输入框
        UserText.setBounds(700,200,180,20);
        MimaText.setBounds(700,240,180,20);

        //1.3将容器添加至界面
            //文字
         this.getContentPane().add(Login);
          this.getContentPane().add(LoginText);
            //图片
        this.getContentPane().add(UserLaber);
        this.getContentPane().add(BackgroundLaber);
        this.getContentPane().add(MimaLaber);



            //输入框
        this.getContentPane().add(UserText);
        this.getContentPane().add(MimaText);

        //1.4创建登录按钮

        Loginbutton1.setBounds(765,280,50,50);
        Loginbutton1.setBorderPainted(false);
        Loginbutton1.setContentAreaFilled(false);

        Loginbutton2.setBounds(765,280,50,50);
        Loginbutton2.setBorderPainted(false);
        Loginbutton2.setContentAreaFilled(false);

        this.getContentPane().add(Loginbutton1);
        this.getContentPane().add(Loginbutton2);

        Loginbutton2.setVisible(false);

        //1.5给登录按钮绑定事件
        Loginbutton1.addMouseListener(this);

    }

    //2.登录界面初始化
    private void InitLoginjframe() {

        //2.1设置界面大小
        this.setSize(920,720);

        //2.2设置界面标题
        this.setTitle("院职工工资管理系统--管理员登录");

        //2.3设置界面居中
        this.setLocationRelativeTo(null);

        //2.4设置界面置顶
        this.setAlwaysOnTop(true);

        //2.5设置界面关闭模式
        this.setDefaultCloseOperation(3);

        //2.6取消容器默认布局
        this.setLayout(null);

        //2.7设置窗口不可以缩放
        this.setResizable(false);

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        //获取密码输入框中的文本
        StringBuilder t = new StringBuilder() ;
        for (int i = 0; i < MimaText.getPassword().length; i++) {
            t.append(MimaText.getPassword()[i]);
        }

        //使用JDBC
        try {
            if(MySQL_IsUserLoginTrue.Logintrue(conn,UserText.getText(),t.toString()))
            {
                //创建弹框
                JDialog Throw = new JDialog();
                new InitJdialog(Throw);
                Throw.setSize(200,100);

                JLabel truethrow = new JLabel("登入成功");
                truethrow.setBounds(65,20,80,20);
                Throw.getContentPane().add(truethrow);
                Throw.setVisible(true);

                this.dispose();
                new MainJframe();
            }
            else{
                //创建弹框
                JDialog Throw = new JDialog();
                new InitJdialog(Throw);
                Throw.setSize(200,100);

                JLabel truethrow = new JLabel("用户名或密码错误请重新输入");
                truethrow.setBounds(10,20,180,20);
                Throw.getContentPane().add(truethrow);
                Throw.setVisible(true);

            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }

    }//点击

    @Override
    public void mousePressed(MouseEvent e) {
        //按压
        Loginbutton1.setVisible(false);

        Loginbutton2.setVisible(true);
    }//按压

    @Override
    public void mouseReleased(MouseEvent e) {
        //结束
        Loginbutton1.setVisible(true);
        Loginbutton2.setVisible(false);
    }//结束

    @Override
    public void mouseEntered(MouseEvent e) {
        //划入


    }//划入

    @Override
    public void mouseExited(MouseEvent e) {
        //划出
    }//划出
}
